package com.iss.itemplate.service.imp;

import com.iss.itemplate.service.GenericService;
import com.iss.itemplate.service.PayPalService;
import com.iss.itemplate.util.PaypalFunctions;
import org.springframework.stereotype.Service;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.HashMap;
import java.util.Map;

/**
 * Created by isoft-work 5 on 11/20/14.
 */
@Service("payPalService")
public class PayPalServiceImp extends GenericService implements PayPalService {
   public void checkOut(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
      String paymentAmount = "15";
      String contextPath = request.getContextPath();
      String returnURL = "https://localhost:8443/"+ contextPath +"/success";

      String cancelURL = "https://localhost:8443/"+ contextPath +"/cancel";

      Map item = new HashMap();
      item.put("name", "item name");
      item.put("amt", paymentAmount);
      item.put("qty", "1");

      PaypalFunctions ppf = new PaypalFunctions();
      HashMap nvp = ppf.setExpressCheckout(paymentAmount, returnURL,
            cancelURL, item);
      String strAck = nvp.get("ACK").toString();
      if (strAck != null && strAck.equalsIgnoreCase("Success")) {

         // ' Redirect to paypal.com
//         String redirectURL = "https://www.sandbox.paypal.com/incontext?token="
//               + nvp.get("TOKEN").toString();
//
//         response.sendRedirect(redirectURL);

         ppf.RedirectURL(response, nvp.get("TOKEN").toString());

      } else {
         // Display a user friendly Error on the page using any of the
         // following error information returned by PayPal

         String ErrorCode = nvp.get("L_ERRORCODE0").toString();
         String ErrorShortMsg = nvp.get("L_SHORTMESSAGE0").toString();
         String ErrorLongMsg = nvp.get("L_LONGMESSAGE0").toString();
         String ErrorSeverityCode = nvp.get("L_SEVERITYCODE0").toString();
      }
   }
}
